# Maintainer: Frederik Schwan <freswa at archlinux dot org>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>
# Contributor: Allan McRae <allan@archlinux.org>
# Contributor: Andreas Radke <andyrtr@archlinux.org>

pkgname=db5.3
pkgver=5.3.28
pkgrel=4
pkgdesc="The Berkeley DB embedded database system v5.3"
arch=(loong64 x86_64)
url='https://www.oracle.com/technology/software/products/berkeley-db/index.html'
license=(custom:sleepycat)
depends=(gcc-libs sh)
source=(
  https://download.oracle.com/berkeley-db/db-${pkgver}.tar.gz
  db-5.3.21-memp_stat-upstream-fix.patch
  db-5.3.21-mutex_leak.patch
  db-5.3.28-lemon_hash.patch
  db-5.3.28_cve-2019-2708.patch
  db-5.3.28-mmap-high-cpu-usage.patch
  db-5.3.28-atomic_compare_exchange.patch
)
b2sums=('5b77a4ec0945fa96ce6c2b452fb4b029295e037fa64d5ff8dcb3b5e01ee3501fd75d9a73b8f5fa54d8907717d93546fbb5348ee70119e877fa2f17d301ecf50d'
        'cfbb212c60c0e75c0d8c1cdbee9bcf0be9aec718ab462f779d5b880a4f976de1423279adde20159eef067be95d0d725eaa0cb950d0a8aaae720de78b94299e72'
        'ba917cf9b69de39337b7141bf145153ae89849921c79b4d2f40734feefe2c0007abcc8c85ed776227875c5102354d8f61069fcee12c7db3b45c4ccabc6993760'
        '682539a382f4423ad4404ad2aa446356d97485a7883a26d31cc2061611b138f20e39d941c575b8997538c4dd609e3e0343505759bba3b89c669ad1830cac1fbf'
        'bc72ba9703cdbabbece3480fd36c22fca26f270cc4b8c66f8f19799b5120010dfdf53772a94374e45d38b25bb8a271d29ea84318aa8bc847a88940d52ee22a8f'
        'bc50b556653a4e33073ed4cbaf716035f0840af2659bb4d6ef900621f92515d9490a698cec70546bd3aededa02b264fcd6e6cba3c4e6412125d2d587094f872d'
        'c32c5236d5f1c9c112e5c6d7aee8d1d67e4f80c3826020ba71d864649bd12f2402a6d68334cfbf1bc17c920878bf64296d884436608a5b3fb9a27ba1fd4d9c75')

# All Patches retrieved from Fedora
prepare() {
  cd db-${pkgver}
  # memp_stat fix provided by upstream
  patch -Np1 -i ../db-5.3.21-memp_stat-upstream-fix.patch
  # fix for mutexes not being released provided by upstream
  patch -Np1 -i ../db-5.3.21-mutex_leak.patch
  # fix for overflowing hash variable inside bundled lemon
  patch -Np1 -i ../db-5.3.28-lemon_hash.patch
  # cve-2019-2708 fixed by mmuzila
  patch -Np1 -i ../db-5.3.28_cve-2019-2708.patch
  # Prevents high CPU usage
  patch -Np1 -i ../db-5.3.28-mmap-high-cpu-usage.patch
  # gcc fix
  patch -Np1 -i ../db-5.3.28-atomic_compare_exchange.patch
}


build() {
  cd db-${pkgver}/build_unix
  ../dist/configure \
    --prefix=/usr \
    --bindir=/usr/bin/db5.3 \
    --includedir=/usr/include/db5.3 \
    --enable-compat185 \
    --enable-shared \
    --disable-static \
    --enable-cxx \
    --enable-dbm \
    --enable-stl
  make LIBSO_LIBS=-lpthread
}

package() {
  make -C db-${pkgver}/build_unix DESTDIR="${pkgdir}" install
  install -Dm644 db-${pkgver}/LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
  install -d "${pkgdir}"/usr/lib/db5.3
  rm "${pkgdir}"/usr/lib/libdb.so
  rm "${pkgdir}"/usr/lib/libdb_cxx.so
  rm "${pkgdir}"/usr/lib/libdb_stl.so
  ln -s ../libdb-5.3.so "${pkgdir}"/usr/lib/db5.3/libdb.so
  ln -s ../libdb_cxx-5.3.so "${pkgdir}"/usr/lib/db5.3/libdb_cxx.so
  ln -s ../libdb_stl-5.3.so "${pkgdir}"/usr/lib/db5.3/libdb_stl.so
  rm -r "${pkgdir}"/usr/docs
}
